package com.car.order.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.car.commons.core.model.Result;
import com.car.order.client.CarAccountClient;
import com.car.order.client.model.clerk.ClerkDto;
import com.car.order.dto.serveOrderLog.ServeOrderLogDto;
import com.car.order.entity.form.serveOrderLog.ServeOrderLogFindListForm;
import com.car.order.entity.form.serveOrderLog.ServeOrderLogFindPageForm;
import com.car.order.entity.form.serveOrderLog.ServeOrderLogInsertForm;
import com.car.order.entity.form.serveOrderLog.ServeOrderLogUpdateForm;
import com.car.commons.core.model.WoniuException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.car.order.mapper.ServeOrderLogMapper;
import com.car.order.entity.ServeOrderLog;
import com.car.order.service.ServeOrderLogService;
import org.springframework.stereotype.Service;
import lombok.extern.slf4j.Slf4j;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author 杜岷芫
 * @since 2024-08-29 10:27:10
 */
@Service
@Slf4j
public class ServeOrderLogServiceImpl extends ServiceImpl<ServeOrderLogMapper, ServeOrderLog> implements ServeOrderLogService {

    @Resource
    private ServeOrderLogMapper serveOrderLogMapper;
    @Resource
    private CarAccountClient carAccountClient;

    @Override
    public Page<ServeOrderLog> findPage(ServeOrderLogFindPageForm form) {
        Page<ServeOrderLog> page = new Page<>(form.getPageNum()==null?1:form.getPageNum(),form.getPageSize()==null?6:form.getPageSize());
        QueryWrapper<ServeOrderLog> query = new QueryWrapper<>();
        return serveOrderLogMapper.selectPage(page, query);
    }

    @Override
    public List<ServeOrderLogDto> findList(ServeOrderLogFindListForm form){
        QueryWrapper<ServeOrderLog> query = new QueryWrapper<>();
        query.eq("serve_order_id", form.getOrderId());
        List<ServeOrderLog> serveOrderLogs = serveOrderLogMapper.selectList(query);
        Set<Integer> clerkIds = serveOrderLogs.stream().map(ServeOrderLog::getLogUser).filter(o->o!=null).collect(Collectors.toSet());
        HashMap<Integer, ClerkDto> clerkDtoHashMap = new HashMap<>();
        if (clerkIds.size() != 0) {
            Result<List<ClerkDto>> clerksResult = carAccountClient.findsClerksByIds(clerkIds);
            if (clerksResult.getCode() != 200) {
                throw new WoniuException(clerksResult.getCode(), clerksResult.getMsg());
            }
            List<ClerkDto> clerkDtos = clerksResult.getData();
            if (clerkDtos == null || clerkDtos.size() != clerkIds.size()) {
                throw new WoniuException(500, "有门店管理员没查到");
            }

            for (ClerkDto clerkDto : clerkDtos) {
                clerkDtoHashMap.put(clerkDto.getClerkId(), clerkDto);
            }
        }
        List<ServeOrderLogDto> serveOrderLogDtos = new ArrayList<>();
        for (ServeOrderLog serveOrderLog : serveOrderLogs) {
            ServeOrderLogDto serveOrderLogDto = BeanUtil.toBean(serveOrderLog, ServeOrderLogDto.class);
            ClerkDto clerkDto = clerkDtoHashMap.get(serveOrderLogDto.getLogUser());
            serveOrderLogDto.setLogUserName(clerkDto==null?"用户":clerkDto.getClerkName());
            serveOrderLogDtos.add(serveOrderLogDto);
        }
        return serveOrderLogDtos;
    }

    @Override
    public ServeOrderLog findById(Integer id) {
        return serveOrderLogMapper.selectById(id);
    }

    @Override
    public ServeOrderLog insert(ServeOrderLogInsertForm form) {
        ServeOrderLog serveOrderLog = BeanUtil.toBean(form, ServeOrderLog.class);
        boolean save = save(serveOrderLog);
        if (!save) {
            throw new WoniuException(500, "新增失败");
        }
        return serveOrderLog;
    }

    @Override
    public ServeOrderLog update(ServeOrderLogUpdateForm form) {
        ServeOrderLog serveOrderLog = BeanUtil.toBean(form, ServeOrderLog.class);
        boolean b = updateById(serveOrderLog);
        if (!b) {
            throw new WoniuException(500, "修改失败");
        }
        return serveOrderLog;
    }

    @Override
    public void delete(Integer id) {
        int i=serveOrderLogMapper.deleteById(id);
        if (i == 0) {
            throw new WoniuException(500, "删除失败");
        }
    }

}